// ITI 1120 Winter 2009, Lab 8 - Example 1
// Name: Dinesh B, Student# 1234567

/**
 * A recursive algorithm for counting the number of digits in a non-negative
 * integer, N.
 */

class NumberOfDigits2
{
    public static void main (String[] args)
    {
    	
        // DECLARE VARIABLES/DATA DICTIONARY 

        int n;         // GIVEN:  a non-negative integer
        int result;    // RESULT: the number of digits in n
        
        // PRINT OUT IDENTIFICATION INFORMATION
        

        // READ IN GIVENS

        System.out.print("Please enter an integer: ");
        n = ITI1120.readInt( );
        
        // BODY OF ALGORITHM
     
        result = countDigits( n );      

        // PRINT OUT RESULTS AND MODIFIEDS

        System.out.println("The integer has " + result + " digits");

    }

    // If the 'main' method calls other algorithms, put the method(s) below.

    /**
     * Returns the number of digits in an integer.
     *
     * @param n An integer for which to find the number of digits.
     * @result The number of digits in <code>n</code>.
     */

    public static int countDigits( int n )
    {
        // DECLARE VARIABLES/DATA DICTIONARY 
        
        int count;
        int restOfDigits;
        
        // BODY OF ALGORITHM
        
        System.out.println( "1: Entering method with n = " + n );
        
        restOfDigits = n / 10;
        if (restOfDigits == 0)
        {
            System.out.println("5: Base case with n = " + n );
            count = 1;
        }
        else
        {
            System.out.println("2: Recursive call from n =  " + n );
            count = countDigits( restOfDigits );
            System.out.println("3: Returned from recursion with n = " + n );
            count = count + 1;
        }
        
        // RETURN RESULT
        
        System.out.println( "4: Returning from method with n = " + n + " , count = " + count );
        return count;
    }   

} 
